package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.template.DestinationTemplate;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import cn.wolfcode.luowowo.search.template.TravelTemplate;
import cn.wolfcode.luowowo.search.template.UserInfoTemplate;
import cn.wolfcode.luowowo.search.vo.SearchResultVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class SearchController {

    @Reference
    private IStrategyTemplateSearchService strategySearchService;

    @Reference
    private IDestinationTemplateSearchService destinationSearchService;

    @Reference
    private ITravelTemplateSearchService travelSearchService;

    @Reference
    private IUserInfoTemplateSearchService userSearchService;

    @Reference
    private ISearchService searchService;

    @RequestMapping("/q")
    public String search(Model model, @ModelAttribute("qo") SearchQueryObject qo){
        if(qo.getType() == SearchQueryObject.TYPE_DEST){
            return searchDest(model,qo);
        }else if(qo.getType() == SearchQueryObject.TYPE_STRATEGY){
            return searchStrategy(model,qo);
        }else if(qo.getType() == SearchQueryObject.TYPE_TRAVEL){
            return searchTravel(model,qo);
        }else if(qo.getType() == SearchQueryObject.TYPE_USER){
            return searchUser(model,qo);
        }
        return searchAll(model,qo);
    }

    private String searchDest(Model model,SearchQueryObject qo){
        DestinationTemplate dest = destinationSearchService.findByName(qo.getKeyword());
        model.addAttribute("dest",dest);
        SearchResultVO resultVO = new SearchResultVO();
        if(dest != null){
            Long total = 0L;
            List<StrategyTemplate> strategyTemplates = strategySearchService.findByDestName(qo.getKeyword());
            total += strategyTemplates.size();
            if(strategyTemplates.size() > 5){
                strategyTemplates.subList(0,5);
            }
            List<TravelTemplate> travelTemplates = travelSearchService.findByDestName(qo.getKeyword());
            total += travelTemplates.size();
            if (travelTemplates.size() > 5){
                travelTemplates.subList(0,5);
            }
            List<UserInfoTemplate> userInfoTemplates = userSearchService.findByDestName(qo.getKeyword());
            total += userInfoTemplates.size();
            if(travelTemplates.size() > 5){
                travelTemplates.subList(0,5);
            }
            resultVO.setStrategys(strategyTemplates);
            resultVO.setTravels(travelTemplates);
            resultVO.setUsers(userInfoTemplates);
            resultVO.setTotal(total);
            model.addAttribute("data",resultVO);
        }
        return "index/searchDest";
    }
    private String searchStrategy(Model model,SearchQueryObject qo){
        Page<StrategyTemplate> page = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle", "summary");
        model.addAttribute("page",page);
        return "index/searchStrategy";
    }
    private String searchTravel(Model model,SearchQueryObject qo){
        Page<TravelTemplate> page = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary");
        model.addAttribute("page",page);
        return "index/searchTravel";
    }
    private String searchUser(Model model,SearchQueryObject qo){
        Page<UserInfoTemplate> page = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class, qo, "nickname", "destName");
        model.addAttribute("page",page);
        return "index/searchUser";
    }


    private String searchAll(Model model,SearchQueryObject qo){
        SearchResultVO resultVO = new SearchResultVO();
        Long total = 0L;
        Page<StrategyTemplate> strategyPage = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle", "summary");
        total += strategyPage.getTotalElements();
        Page<TravelTemplate> travelPage = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary");
        total += travelPage.getTotalElements();
        Page<UserInfoTemplate> userPage = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class, qo, "nickname", "destName");
        total += userPage.getTotalElements();
        Page<DestinationTemplate> destPage = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME, DestinationTemplate.TYPE_NAME,
                DestinationTemplate.class, qo, "info", "name");
        total += destPage.getTotalElements();
        resultVO.setTotal(total);
        resultVO.setDests(destPage.getContent());
        resultVO.setStrategys(strategyPage.getContent());
        resultVO.setTravels(travelPage.getContent());
        resultVO.setUsers(userPage.getContent());
        model.addAttribute("data",resultVO);
        return "index/searchAll";
    }


}
